﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;

using Fuse8.DomainFramework.Domain;
using Fuse8.DomainFramework.Common;
using Fuse8.DomainFramework.BusinessService;

namespace Fuse8.ContentManagementFramework.Domain
{
    public partial class CmsContentType
    {
        #region Fields

        public static readonly string SnippetSystemContentType = "Snippet";
        public static readonly string PageContentSystemContentType = "PageContent";

        #endregion

        #region Methods

        public static IEnumerable<CmsContentType> GetSystemContentTypes(CmsSite cmsSite)
        {
            Guard.ArgumentNotNull(cmsSite, "cmsSite");

            return new BusinessServiceFacade<CmsContentType, int>()
                .FindAll()
                .Where(p => p.IsSystemType);
        }

        public static IEnumerable<CmsContentType> GetCmsContentTypes()
        {
            return new BusinessServiceFacade<CmsContentType, int>()
                .FindAll()
                .Where(p => !p.IsSystemType);
        }

        #endregion

        #region Constructor

        public CmsContentType(string typeValue)
            : this()
        {
            var contentType = new BusinessServiceFacade<CmsContentType, int>()
                .FindOne(p => p.ContentTypeName == typeValue);

            if (contentType.IsCreated)
            {
                this.Id = contentType.Id;
                this.ContentTypeName = contentType.ContentTypeName;
                this.Description = contentType.Description;                
                this.IsSystemType = contentType.IsSystemType;                
                this.SiteGroupId = contentType.SiteGroupId;
                this.SiteId = contentType.SiteId;
                this.CreatedBy = contentType.CreatedBy;
                this.DateCreated = contentType.DateCreated;
                this.LastUpdatedBy = contentType.LastUpdatedBy;
                this.DateLastUpdated = contentType.DateLastUpdated;
            }
            else
            {
                //TO DO
                throw new Exception();
            }
        }

        #endregion

        [XmlIgnore]
        public string UserCreatedBy { get { return (CreatedByUser ?? new CmsAnonimousUser()).UserName; } }

        [XmlIgnore]
        public string UserLastUpdatedBy { get; set; }

        [XmlIgnore]
        public CmsUser CreatedByUser { get; set; }
    }
}
